package com.useful.core.config;

import com.useful.core.component.FtpComponent;
import com.useful.core.prop.FtpProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;

/**
 * @author : wenchao.long
 * @date : Created in 2021/9/20 22:06 周一
 * @description :
 */
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties({FtpProperties.class})
public class SshConfig {

    private final FtpProperties ftpProperties;

    @Bean
    public FtpComponent ftpComponent() {
        return new FtpComponent(ftpProperties);
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 单个文件大小
        factory.setMaxFileSize(DataSize.ofMegabytes(ftpProperties.getMaxFileSize()));
        // 总上传文件大小
        factory.setMaxRequestSize(DataSize.ofMegabytes(ftpProperties.getMaxRequestSize()));
        return factory.createMultipartConfig();
    }
}
